

import java.awt.Graphics;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Random;


public class NPC extends Person implements Drawable{

        private ArrayList<String> textList;
        private int numChars = 50, currentLine = 0, delay = 0, location = 0, direction = 0, facing = 0;
       

        private Bubble bubble = null;
        private ArrayList<Bubble> bubbleList;
        private static Random r = new Random();

        public ArrayList<Bubble> getBubbleList() {
                return bubbleList;
        }

        public void setBubbleList(ArrayList<Bubble> bubbleList) {
                this.bubbleList = bubbleList;
        }

        public NPC(int x, int y, String text)
        {
                this.setX(x);
                this.setY(y);
                this.setXOffset(0);
                this.setYOffset(0);
                this.setAnimations(new HashMap<String, Animation>());
                parseString(text);

                //Add the animations to the hashmap
                //We need a walking animation for - Left, Right, Up, Down

        }

        public void update() {
                if(OurApplet.talking == false)
                {
                        if (!(this instanceof Sign || this instanceof SolonManStationary)) {
                                delay = (delay + 1)%30;
                                //direction is the direction they are moving in, zero if it is not moving
                                if (delay == 0 && direction == 0) {
                                        switch (location) {
                                        //This means the npc is in the center of the + and they can choose to go in any of these four directions
                                        case 0 :
                                                switch (r.nextInt(4) + 1) {
                                                case 1 :
                                                        if (r.nextInt(4) == 0) {
                                                                direction = 1;
                                                                location = 1;
                                                                facing = 1;
                                                                setCurrentAnimation("RIGHT");
                                                                getCurrentAnimation().start();
                                                        }
                                                        break;
                                                case 2 :
                                                        if (r.nextInt(4) == 0) {
                                                                direction = 2;
                                                                location = 2;
                                                                facing = 2;
                                                                setCurrentAnimation("UP");
                                                                getCurrentAnimation().start();
                                                        }
                                                        break;
                                                case 3 :
                                                        if (r.nextInt(4) == 0) {
                                                                direction = 3;
                                                                location = 3;
                                                                facing = 3;
                                                                setCurrentAnimation("LEFT");
                                                                getCurrentAnimation().start();
                                                        }
                                                        break;
                                                case 4 :
                                                        if (r.nextInt(4) == 0) {
                                                                direction = 4;
                                                                location = 4;
                                                                facing = 4;
                                                                setCurrentAnimation("DOWN");
                                                                getCurrentAnimation().start();
                                                        }
                                                        break;
                                                }
                                                break;
                                        //These four cases should send the player back to the center position by moving them in the direction that is the reverse of their position
                                        case 1 :
                                                if (r.nextInt(4) == 0) {
                                                        direction = 3;
                                                        location = 0;
                                                        facing = 3;
                                                        setCurrentAnimation("LEFT");
                                                        getCurrentAnimation().start();
                                                }
                                                break;
                                        case 2 :
                                                if (r.nextInt(4) == 0) {
                                                        direction = 4;
                                                        location = 0;
                                                        facing = 4;
                                                        setCurrentAnimation("DOWN");
                                                        getCurrentAnimation().start();
                                                }
                                                break;
                                        case 3 :
                                                if (r.nextInt(4) == 0) {
                                                        direction = 1;
                                                        location = 0;
                                                        facing = 1;
                                                        setCurrentAnimation("RIGHT");
                                                        getCurrentAnimation().start();
                                                }
                                                break;
                                        case 4 :
                                                if (r.nextInt(4) == 0) {
                                                        direction = 2;
                                                        location = 0;
                                                        facing = 2;
                                                        setCurrentAnimation("UP");
                                                        getCurrentAnimation().start();
                                                }
                                                break;
                                        }
                                }
                            if (direction != 0) move(direction);
                            if (!getMoving()) {
                                    direction = 0;
                                    this.getCurrentAnimation().stop();
                                    this.getCurrentAnimation().setCurrentFrame(0);
                                   
                            }
                        }
                }
                else if(OurApplet.talking == true)
                {
                        int x = this.getX() - World.getInstance().getMap().player.getX();
                        int y = this.getY() - World.getInstance().getMap().player.getY();
                       
                        if(x == 1)
                        {
                                this.facing = 3;
                                this.setCurrentAnimation("LEFT");
                                this.getCurrentAnimation().setCurrentFrame(0);
                        }
                        if(x == -1)
                        {
                                this.facing = 1;
                                this.setCurrentAnimation("RIGHT");
                                this.getCurrentAnimation().setCurrentFrame(0);
                        }
                        if(y == 1)
                        {
                                this.facing = 2;
                                this.setCurrentAnimation("UP");
                                this.getCurrentAnimation().setCurrentFrame(0);
                        }
                        if(y == -1)
                        {
                                this.facing = 4;
                                this.setCurrentAnimation("DOWN");
                                this.getCurrentAnimation().setCurrentFrame(0);
                        }
                }
        }

        private void parseString(String str) {
                textList = new ArrayList<String>();
                int startPos = 0, endPos = numChars;
                while (startPos < str.length() - numChars) {
                        char c = str.charAt(endPos - 1);
                        char d = str.charAt(endPos);
                        textList.add(str.substring(startPos, endPos) + (c == ' ' || c == '.' || c == ',' || c == '!' || c == '?' || c == '-' || d == ' ' ? "" : "-"));
                        startPos += numChars;
                        endPos += numChars;
                }
                textList.add(str.substring(startPos));
                bubbleList = new ArrayList<Bubble>();
                bubbleList.add(null);
                int i = 0;
                for (; i < textList.size() - 1; i += 2) {
                        bubbleList.add(new Bubble(textList.get(i), textList.get(i + 1)));
                }
                if (textList.size()%2 == 1) {
                        bubbleList.add(new Bubble(textList.get(i), ""));
                }
        }

        public int getCurrentLine() {
                return currentLine;
        }

        public void setCurrentLine(int currentLine) {
                this.currentLine = currentLine;
        }
       
        public void updateText() {
                currentLine = (currentLine + 1)%bubbleList.size();
        }

        public void draw(Graphics g){
//              if (bubbleList.get(currentLine) != null) {
//                      OurApplet.talking = true;
//                      bubbleList.get(currentLine).draw(g);
//              } else if (OurApplet.talking == true) {
//                      OurApplet.talking = false;
//              }
                //if (!(this instanceof Sign)) super.draw(g);
        }
}
